package com.nep.NEPG.service;

import com.nep.NEPG.model.AQIData;
import com.nep.NEPG.repository.AQIDataRepository;
import java.util.List;

public class AQIService {
    private final AQIDataRepository aqiDataRepository;
    private final AuthService authService;

    public AQIService(AQIDataRepository aqiDataRepository, AuthService authService) {
        this.aqiDataRepository = aqiDataRepository;
        this.authService = authService;
    }

    /**
     * 获取当前用户关联的AQI数据
     */
    public List<AQIData> getMyAQIData() {
        String currentUserId = authService.getCurrentUser().getId();
        return aqiDataRepository.findByUserId(currentUserId);
    }

    /**
     * 提交AQI数据（标记为已提交状态）
     */
    public void submitAQIData(String aqiDataId) {
        AQIData data = aqiDataRepository.findById(aqiDataId);
        if (data != null) {
            data.setSubmitted(true);
            aqiDataRepository.save(data);
        }
    }

    /**
     * 创建新的AQI数据记录
     */
    public void createAQIData(String taskId, double pm25, double pm10,
                              double so2, double no2, double o3, double co) {
        AQIData newData = new AQIData();
        newData.setTaskId(taskId);
        newData.setPm25(pm25);
        // 设置其他字段...
        newData.setSubmitted(false);

        aqiDataRepository.save(newData);
    }

    /**
     * 更新已有的AQI数据
     */
    public void updateAQIData(AQIData aqiData) {
        aqiDataRepository.save(aqiData);
    }
}